package com.gosophia.conference.action.scheme;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.scheme.dto.SchemeJobDataDTO;
import com.gosophia.conferenceManagement.scheme.dto.SchemeTransientStoreDTO;
import com.gosophia.conferenceManagement.scheme.ws.ConferenceSchemeWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.opensymphony.xwork2.ActionContext;

/**
 * 方案起草提交Action，此Action用于方案起草页面【提交】按钮
 * @author nana
 *
 */
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("SchemeSimulateSubmitAction")
public class SchemeSimulateSubmitAction extends BaseSupportAction{

	private Long jobId;
	
	private SchemeTransientStoreDTO schemeTransientStoreDTO;
	
	@Autowired
	private ConferenceSchemeWebService conferenceSchemeWebService;
	@Autowired
    private JudgeCurrentUndertaker judgeCurrentUndertaker;
	
	private String onlinePath;
	private String onlineVersion;
	@Override
	public String _execute() throws Exception {
	    
	    //判断当前用户和job承办人是否匹配,如果不匹配转到异常页面
	    if(jobId!=null){
	        judgeCurrentUndertaker.judgeOperationAuthority(jobId);
	    }
	    
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
		UserInfoDTO userInfoDTO = userDetails.getUserAccountDTO().getUserInfoDTO();
		
		SchemeJobDataDTO schemeJobDataDTO = new SchemeJobDataDTO();
		if(schemeTransientStoreDTO.getCreatorDTO()==null){
		    schemeJobDataDTO.setConferenceCreatorDTO(userInfoDTO);
		}else{
		    schemeJobDataDTO.setConferenceCreatorDTO(schemeTransientStoreDTO.getCreatorDTO());
		}
		schemeJobDataDTO.setConferenceDescription(schemeTransientStoreDTO.getDescription());
		schemeJobDataDTO.setConferenceName(schemeTransientStoreDTO.getConferenceName());
		schemeJobDataDTO.setImportantLevelDTO(schemeTransientStoreDTO.getImportanceDTO());
		schemeJobDataDTO.setOnlineDocumentDTO(schemeTransientStoreDTO.getSchemeDocumentDTO());
        schemeJobDataDTO.setOpinion(schemeTransientStoreDTO.getNote());
        schemeJobDataDTO.setPlanEndTime(schemeTransientStoreDTO.getPlanEndDate());
        DocumentDTO documentDTO = new DocumentDTO();
        documentDTO.setDocumentPath(onlinePath);
        documentDTO.setDocumentVersion(onlineVersion);
        documentDTO.setDocumentCategory("online");
        schemeJobDataDTO.setOnlineDocumentDTO(documentDTO);
		if(schemeTransientStoreDTO.getCommandPersonDTO()!=null){
		    schemeJobDataDTO.setNextApproval(schemeTransientStoreDTO.getCommandPersonDTO());
		}else{
		    schemeJobDataDTO.setNextApproval(userInfoDTO);
		}
		
		if("SUBMIT_APPROVAL".equals(schemeTransientStoreDTO.getNextStep())){
		    schemeJobDataDTO.setTurningAction(ConferenceTurningAction.SUBMIT_APPROVAL.getValue());
		}else if("DIRECT_FINALIZED".equals(schemeTransientStoreDTO.getNextStep())){
		    schemeJobDataDTO.setTurningAction(ConferenceTurningAction.DIRECT_FINALIZED.getValue());
		}else if("JUMP_ACTION".equals(schemeTransientStoreDTO.getNextStep())){
		    schemeJobDataDTO.setTurningAction(ConferenceTurningAction.JUMP_ACTION.getValue());
		}
		if(jobId!=null){
			conferenceSchemeWebService.HandleConferenceSchemeTurning(jobId, userInfoDTO.getUserInfoId(), schemeJobDataDTO);
		}else{
			conferenceSchemeWebService.startingConferenceProcessing(schemeJobDataDTO);
		}
		
		//提交审批成功后将暂存删除(若数据库中有的话)
		if(schemeTransientStoreDTO.getSchemeTransientStoreId()!=null){
			if(schemeTransientStoreDTO.getJobId()!=null){
				SchemeTransientStoreDTO	schemeTransientStoreDTO =	conferenceSchemeWebService.findTransientStoreByJobId(jobId);
				System.out.println(schemeTransientStoreDTO.getSchemeTransientStoreId()+"dddddddd");
				conferenceSchemeWebService.deleteSchemeTransientStore(schemeTransientStoreDTO.getSchemeTransientStoreId());
			}else{
		        conferenceSchemeWebService.deleteSchemeTransientStore(schemeTransientStoreDTO.getSchemeTransientStoreId());
			}
		}else{
			
		}
		
		super.getRequestResult().setRsltCode("0");
		super.getRequestResult().setMessage("会议方案起草提交成功");
		ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());
		if("task".equals(this.getFromFlag())){
			return "TASK";
		}
		return SUCCESS;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getJobId() {
		return jobId;
	}

    public SchemeTransientStoreDTO getSchemeTransientStoreDTO() {
        return schemeTransientStoreDTO;
    }

    public void setSchemeTransientStoreDTO(
            SchemeTransientStoreDTO schemeTransientStoreDTO) {
        this.schemeTransientStoreDTO = schemeTransientStoreDTO;
    }

    public String getOnlinePath() {
        return onlinePath;
    }

    public void setOnlinePath(String onlinePath) {
        this.onlinePath = onlinePath;
    }

    public String getOnlineVersion() {
        return onlineVersion;
    }

    public void setOnlineVersion(String onlineVersion) {
        this.onlineVersion = onlineVersion;
    }


}
